<?php
/**
 * 表单类模型
 *
 *@author hary <hary@taomee.com>
 */
class form_model extends model
{
	private $form_table;
	private $entry_table;

	function __construct()
	{
		parent::__construct();

		$this->form_table   = 'form';
		$this->entry_table  = 'entry';
	}

	/**
	 * 根据客户端传到的信息进行整个form保存
	 * @return unknown_type
	 */
	public function saveForm($formKey,$obj){

		if(empty($formKey)) return false;

		$FormTitle 		 = $this->formatData($obj->FormTitle);
		$FormDescription = $this->formatData($obj->FormDescription);
		$FormKey 		 = $formKey;
		$Entries 		 = $obj->Entries;
		$FormId			 = intval($obj->FormId);
		$FormHeight		 = intval($obj->FormHeight);
		$FormWidth		 = intval($obj->FormWidth);
		$FormQesNum 	 = intval($obj->FormQesNum);

		$form = array("f_title"=>$FormTitle,"f_des"=>$FormDescription,"f_question_num"=>$FormQesNum,"f_key"=>$FormKey,"f_id"=>$FormId,"f_width"=>$FormWidth,"f_height"=>$FormHeight);

		try{
			$add_result   = $this->changeFormItemByKey($form);
			$removeOption = $obj->removeOption;

			foreach($removeOption as $entry_id)
			{
				$this->delEntryItemByFormEntryKey($FormKey,$entry_id);
			}

			foreach($Entries as $entry)
			{
				$EntryId 	   = intval($entry->EntryId);
				$QuestionTitle = $entry->QuestionTitle;
				$HelpText      = $entry->HelpText;
				$EntryType     = intval($entry->EntryType);
				$Required	   = intval($entry->Required);
				$Position	   = intval($entry->Position);
				$Choices       = json_encode($entry->Choices);
				$is_new		   = intval($entry->is_new);
				$is_Goto	   = intval($entry->is_Goto);

				$entry_ = array("e_required"=>$Required,
								"e_question_title"=>trim($QuestionTitle),
								"e_position"=>$Position,
								"e_form_key"=>$FormKey,
								"e_help_text"=>trim($HelpText),
								"e_type"=>$EntryType,
								"e_choices"=>$Choices,
								"e_entry_id"=>$EntryId,
								"e_isGoto"=>$is_Goto
				);

				if($is_new)
				{
					$this->addEntryItem($entry_);
				}
				else
				{
					$this->changeEntryItemByFormEntryId($entry_);
				}
			}

		}catch(Exception $e)
		{
			return false;
		}

		return true;

	}

	/**
	 * 添加上传文件
	 * @param $banner
	 * @return max
	 */
	public function uploadFile($file){

		$this->loadModel("upload_model");

		$ret = $this->upload_model->upload('upload_file','upload');

		$banner['b_file_type']  = $ret['type'];
		$banner['b_width'] 	    = $ret['width'];
		$banner['b_height'] 	= $ret['height'];
		$banner['b_Dimensions'] = $ret['width'].' x '.$ret['height'];
		$banner['b_code']  	    = $ret['name'];
		$banner['b_url']		= $ret['url'];
		$banner['b_path']		= $ret['path'];
		$banner['b_update_date'] = time();

		$this->addDataBatch2($banner,array('b_name','b_site','b_start_time','b_end_time','b_forever','b_mtype','b_files','b_des','sort'));

		$result = $this->dataInsert($this->ad_banner_table);

		if($result) $banner['b_id'] = $this->db_insert_id;

		return $result?$banner:$result;
	}

	/**
	 * 通过formkey获取整个表单的数据
	 * @return unknown_type
	 */
	public function getFormByKey($key){

		$key = $this->formatData($key);

		$this->where("f_key",$key);

		$form = $this->getDataInfo($this->form_table);

		if(empty($form)) return false;

		$FormTitle 		 = $form['f_title'];
		$FormDescription = $form['f_des'];
		$FormKey 		 = $form['f_key'];
		$FormId			 = intval($form['f_id']);

		$entries  = array();
		$entries_ = $this->getEntryItemsByFormKey($FormKey);

		foreach($entries_ as $entry)
		{
			$EntryId 	   = intval($entry['e_entry_id']);
			$QuestionTitle = $entry['e_question_title'];
			$HelpText      = $entry['e_help_text'];
			$EntryType     = intval($entry['e_type']);
			$Required	   = intval($entry['e_required']);
			$Position	   = intval($entry['e_position']);
			$is_Goto	   = intval($entry['e_isGoto']);
			$Choices       = is_null(json_decode($entry['e_choices']))?array():json_decode($entry['e_choices']);

			foreach($Choices as $key=>$val)
			{
				$Choices[$key] = $this->object2array($val);
			}

			$arr = array("EntryId"=>$EntryId,
							"QuestionTitle"=>$QuestionTitle,
							"HelpText"=>$HelpText,
							"EntryType"=>$EntryType,
							"Required"=>$Required,
							"Position"=>$Position,
							"Choices"=>$Choices,
							"is_Goto"=>$is_Goto
			);

			array_push($entries,$arr);
		}

		usort($entries,'compare');

		return array("FormTitle"=>$FormTitle,
					 "FormDescription"=>$FormDescription,
					 "FormKey"=>$FormKey,
					 "FormId"=>$FormId,
					 "Entries"=>$entries
		);
	}

	/**
	 * 激活form
	 * @return unknown_type
	 */
	public function activeForm($flag){

	}

	/**
	 * 添加一个表单
	 * @param $form
	 * @return boolean
	 */
	public function addFormItem($form){

		$form = $this->formatData($form);
			
		if(!is_array($form)) return false;

		$embed_url = $this->sys_conf_var['survey']['form']['script'].$form['f_key'];

		$arr['f_title']	 	 	= $form['f_title'];
		$arr['f_key'] 		 	= $form['f_key'];
		$arr['f_start_time'] 	= $form['f_start_time'];
		$arr['f_end_time']   	= $form['f_end_time'];
		$arr['f_theme']      	= $form['f_theme'];
		$arr['f_sort'] 		 	= $form['f_sort'];
		$arr['f_des'] 		 	= $form['f_des'];
		$arr['f_embed_url']  	= $embed_url;
		$arr['f_width']  		= $form['f_width'];
		$arr['f_height']  		= $form['f_height'];
		$arr['f_question_num']  = $form['f_question_num'];

		$this->addData($arr);

		$result = $this->dataInsert($this->form_table);

		return $result;
	}

	/**
	 * 根据form key删除表单
	 * @return unknown_type
	 */
	public function delFormItemByKey($key,$is_casecade){

		$key = $this->formatData($key);

		try{

			if($is_casecade)
			{
				$this->delEntryItemsByFormKey($key);
			}

			$this->where('f_key',$key);

			return $this->dataDel($this->form_table);
		}
		catch(Exception $e)
		{
			return false;
		}

	}

	/**根据id删除表单
	 * @return boolean
	 */
	public function delFormItemById($id,$is_cascade){

		if(!is_numeric($id)) return false;
			
		try{

			if($is_casecade)
			{
				$this->delEntryItemsByFormId($id);
			}
				
			$this->where('f_id',$id,'=','and',0);
				
			return $this->dataDel($this->form_table);

		}
		catch(Exception $e)
		{
			return false;
		}
	}

	/**
	 * 根据id获取获取表单项
	 * @return max
	 */
	public function getFormItemById($id){

		if(!is_numeric($id)) return false;
			
		$this->where('f_id',$id,'=','and',0);
			
		return $this->getDataInfo($this->form_table);
			
	}

	/**
	 * 根据form key获取表单项
	 * @return max
	 */
	public function getFormItemByKey($key){
			
		$key = $this->formatData($key);

		$this->where('f_key',$key);
			
		return $this->getDataInfo($this->form_table);
			
	}

	/**
	 * 更改表单项
	 * @return boolean
	 */
	public function changeFormItemByKey($form){

		$form = $this->formatData($form);
		$key  = $form['f_key'];
			
		if(!is_array($form)) return false;
			
		$embed_url = $this->sys_conf_var['survey']['form']['script'].$form['f_key'];

		$arr['f_title']	 	 	= $form['f_title'];
		$arr['f_key'] 		 	= $form['f_key'];
		
		if(!empty($form['f_start_time']))
		{
			$arr['f_start_time'] 	= $form['f_start_time'];
		}
		
		if(!empty($form['f_end_time']))
		{
			$arr['f_end_time'] 	= $form['f_end_time'];
		}
		
		$arr['f_theme']      	= $form['f_theme'];
		$arr['f_sort'] 		 	= $form['f_sort'];
		$arr['f_des'] 		 	= $form['f_des'];
		$arr['f_embed_url']  	= $embed_url;
		$arr['f_width']  		= $form['f_width'];
		$arr['f_height']  		= $form['f_height'];
		$arr['f_question_num']  = $form['f_question_num'];
			
		$this->addData($arr);
			
		$this->where('f_key',$key);
			
		return $this->dataUpdate($this->form_table);

	}

	/**
	 * 根据关键字获取多个表单
	 * @return max
	 */
	public function getFormItemsByKeywords($keywords,$page='1',$page_size='5',$order='f_sort desc'){

		$keywords = $this->formatData($keywords);
		
		if(empty($keywords))
		{
			$sql = "select * from s_$this->form_table ";
		}
		else
		{
			$sql = "select * from s_$this->form_table where
   				f_title like '%$keywords%' or 
   				f_des like '%$keywords%'  or 
   				f_question_num like '%$keywords%' or 
   				f_embed_url like '%$keywords%'";
		}
			
		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder('order by '.$order);
			
		return $this->customQuery($sql);
	}

	/**
	 * 根据时间进行调查表的查询
	 * @return max
	 */
	public function getFormItemsByTime($start,$end,$page='1',$page_size='10',$order='f_sort desc'){

		if(!is_numeric($start) || !is_numeric($end)) return false;
			
		$sql = "select * from s_$this->form_table where  f_start_time <= $start and f_end_time >= $end ";
			
		if($page) $this->setPage($page);

		if($page_size) $this->setLimit($page_size);

		if($order) $this->setOrder('order by '.$order);

		return $this->customQuery($sql);

	}

	/**
	 * 添加控件
	 * @param $entry
	 * @return boolean
	 */
	public function addEntryItem($entry){

		$entry = $this->formatData($entry);
			
		if(!is_array($entry)) return false;

		$arr['e_required']	 	 = $entry['e_required'];
		$arr['e_question_title'] = $entry['e_question_title'];
		$arr['e_position'] 		 = $entry['e_position'];
		$arr['e_form_key']   	 = $entry['e_form_key'];
		$arr['e_help_text']      = $entry['e_help_text'];
		$arr['e_type']   		 = $entry['e_type'];
		$arr['e_choices'] 		 = $entry['e_choices'];
		$arr['e_entry_id'] 		 = $entry['e_entry_id'];
		$arr['e_isGoto'] 		 = $entry['e_isGoto'];
			
		$this->addData($arr);
			
		return $this->dataInsert($this->entry_table);
	}

	/**
	 * 根据entry key删除控件项
	 * @return unknown_type
	 */
	public function delEntryItemsByFormKey($key){

		$key = $this->formatData($key);

		$this->where('e_form_key',$key);
			
		return $this->dataDel($this->entry_table);
	}

	/**
	 * 根据id删除控件项
	 * @return boolean
	 */
	public function delEntryItemById($id){

		if(!is_numeric($id)) return false;
			
		$this->where('e_id',$id,'=','and',0);
			
		return $this->dataDel($this->entry_table);
	}

	/**
	 * 根据form_key,entry_id删除控件项
	 * @return boolean
	 */
	public function delEntryItemByFormEntryKey($form_key,$entry_id){

		if(!is_numeric($entry_id)) return false;

		$this->where('e_form_key',$form_key);
		$this->where('e_entry_id',$entry_id,'=','and',0);
			
		return $this->dataDel($this->entry_table);
	}

	/**
	 * 根据id获取获取控件项
	 * @return max
	 */
	public function getEntryItemById($id,$is_cascade){

		if(!is_numeric($id)) return false;
			
		$this->where('e_id',$id,'=','and',0);
			
		$result = $this->getDataInfo($this->entry_table);
			
		if($result['e_type'] && $is_cascade) $result['e_type'] = $this->cascadeType($result['e_type']);
			
		return $result;
			
	}

	/**
	 * 根据formkey and id获取获取控件项
	 * @return max
	 */
	public function getEntryItemByFormEntryId($key,$id,$is_cascade){

		$this->where('e_form_key',$key);
		$this->where('e_entry_id',$id,'=','and',0);
			
		$result = $this->getDataInfo($this->entry_table);
			
		if($result['e_type'] && $is_cascade) $result['e_type'] = $this->cascadeType($result['e_type']);
			
		return $result;
			
	}

	/**
	 * 根据form_key获取控件项
	 * @return max
	 */
	public function getEntryItemsByFormKey($key,$is_cascade=false){

		$this->where('e_form_key',$key);

		$this->setOrder(" order by e_position ");
			
		$result = $this->getDataList($this->entry_table);

		if($is_cascade && $result['e_type']) $result['e_type'] = $this->cascadeType($result['e_type']);

		return $result;
			
	}

	/**
	 * 更改控件项
	 * @return boolean
	 */
	public function changeEntryItemById($entry){

		$entry = $this->formatData($entry);
		$id    = $entry['e_id'];
			
		if(!is_array($entry)) return false;
		
		$arr['e_required']	 	 = $entry['e_required'];
		$arr['e_question_title'] = $entry['e_question_title'];
		$arr['e_position'] 		 = $entry['e_position'];
		$arr['e_form_key']   	 = $entry['e_form_key'];
		$arr['e_help_text']      = $entry['e_help_text'];
		$arr['e_type']   		 = $entry['e_type'];
		$arr['e_choices'] 		 = $entry['e_choices'];
		$arr['e_entry_id'] 		 = $entry['e_entry_id'];
		$arr['e_isGoto'] 		 = $entry['e_isGoto'];
			
		$this->addData($arr);
			
		$this->where('e_id',$id);
			
		return $this->dataUpdate($this->entry_table);

	}

	/**
	 * 根据entry_id更改控件项
	 * @return boolean
	 */
	public function changeEntryItemByFormEntryId($entry){

		$entry 	   = $this->formatData($entry);
		$id    	   = $entry['e_entry_id'];
		$form_key  = $entry['e_form_key'];

		if(!is_array($entry)) return false;
			
		$arr['e_required']	 	 = $entry['e_required'];
		$arr['e_question_title'] = $entry['e_question_title'];
		$arr['e_position'] 		 = $entry['e_position'];
		$arr['e_help_text']      = $entry['e_help_text'];
		$arr['e_type']   		 = $entry['e_type'];
		$arr['e_choices'] 		 = $entry['e_choices'];
		$arr['e_isGoto'] 		 = $entry['e_isGoto'];
			
		$this->addData($arr);
			
		$this->where('e_form_key',$form_key);
		$this->where('e_entry_id',$id);
			
		return $this->dataUpdate($this->entry_table);

	}

	/**
	 * 根据关键字获取多个控件项
	 * @return max
	 */
	public function getEntryItemsByKeywords($keywords,$page='1',$page_size='10',$order='e_id desc'){

		$keywords = $this->entryatData($keywords);
			
		$sql = "select * from s_$this->entry_table where
   				e_required like '%$keywords%' or 
   				e_question_title like '%$keywords%' or 
   				e_help_text	 like '%$keywords%'  or 
   				e_type like '%$keywords%'";
			
		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder('order by '.$order);
			
		return $this->customQuery($sql);
	}

	/**
	 * 级联获取控件类型信息
	 * @return max
	 */
	public function cascadeType($typeid){
			
		if(!is_numeric($typeid)) return false;
			
		$this->where('et_id',$typeid,'=','and',0);
			
		return $this->getDataInfo('s_entry_type');

	}

	/**
	 * 点击量修改
	 * @return unknown_type
	 */
	public function addHitCount($key,$hit){

		if(!is_numeric($hit)) return false;
		
		$form 	 = $this->getFormItemByKey($key);
		$hitNum  = intval($form['f_hit'])+$hit;

		$this->addData("f_hit",$hitNum);

		$this->where("f_key",$key);

		$result = $this->dataUpdate($this->form_table);

		return $result;
	}

	/**
	 * 完成量修改
	 * @return unknown_type
	 */
	public function addCompleteHit($key,$complete){

		if(!is_numeric($complete)) return false;

		$form 	   = $this->getFormItemByKey($key);
		$submitNum = intval($form['f_complete'])+$complete;

		$this->addData("f_complete",$submitNum);

		$this->where("f_key",$key);

		$result = $this->dataUpdate($this->form_table);

		return $result;
	}
	
}